<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
</head>

<body>

  <!-- vm.$emoit(自定义事件属性名，子组件数据。) -->
  <div id="app">
    <cpn @itemclick="cpnClick"></cpn>
  </div>
  <template id="cpn">
    <div>
      <button v-for="(item, index) in categories" :key="item.id" @click="btnClick(item.id)">{{item.name}}</button>
    </div>
  </template>
  <script>
    const cpn = {
      template: "#cpn",
      data() {
        return {
          categories: [
            { id: 1, name: '热门推荐' },
            { id: 2, name: '手机数码' },
            { id: 3, name: '家店用品' },
            { id: 4, name: '办公用品' }
          ]
        }
      },
      methods: {
        btnClick(id) {
          this.$emit('itemclick',id)//触发自定义事件itemclick事件属性。
        }
      },

    }
    const app = new Vue({
      el: "#app",
      data() {
        return {
          sonID:''
        }
      },
      methods: {
        cpnClick(id){
          console.log(id)
          this.sonID=id
        }
      },
      components: {
        cpn
      }
    })
  </script>
</body>

</html>